#pragma once
#include <stdint.h>
#include "gpio.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
#include "driver/uart.h"
#include "driver/uart_select.h"
#include "pin_config.h"

#ifndef __STATIC_INLINE
#define __STATIC_INLINE static inline
#endif

#ifndef __WEAK
#define __WEAK __attribute__((weak))
#endif

#ifdef MDLINK
#define PIN_SWCLK_TCK GPIO_NUM_12
#define PIN_SWDIO_TMS GPIO_NUM_11
#define PIN_TDI       GPIO_NUM_15
#define PIN_TDO       GPIO_NUM_13
#define PIN_nRESET    GPIO_NUM_8
#define PIN_UART_TX   GPIO_NUM_5
#define PIN_UART_RX   GPIO_NUM_6
#define PIN_RST       GPIO_NUM_7
#define PIN_BOOT      GPIO_NUM_4
#else
#define PIN_SWCLK_TCK MDLINK_SWCLK_TCK
#define PIN_SWDIO_TMS MDLINK_SWDIO_TMS
#define PIN_TDI       MDLINK_TDI
#define PIN_TDO       MDLINK_TDO
#define PIN_nRESET    MDLINK_nRESET
#define PIN_UART_TX   MDLINK_UART_TX
#define PIN_UART_RX   MDLINK_UART_RX
#define PIN_RST       MDLINK_RST
#define PIN_BOOT      MDLINK_BOOT
#endif // MDLINK
// #define LED_CONNECTED               GPIO_NUM_41
// #define LED_RUNNING                 GPIO_NUM_42
#ifdef MDLINK
#define DAP_UART_TX GPIO_NUM_4
#define DAP_UART_RX GPIO_NUM_5
#else
#define DAP_UART_TX  MdLink_DAP_UART_TX
#define DAP_UART_RX  MdLink_DAP_UART_RX
#endif // MDLINK

#define USART_UX     UART_NUM_0
#define DEV_UART0_TX GPIO_NUM_16
#define DEV_UART0_RX GPIO_NUM_3
#define RX_BUF_SIZE  1024

/// Processor Clock of the Cortex-M MCU used in the Debug Unit.
/// This value is used to calculate the SWD/JTAG clock speed.
#define CPU_CLOCK CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ * 1000000U ///< Specifies the CPU Clock in Hz.
//#define DAP_CPU_CLOCK 240000000U

void dap_platform_init(void);
void dap_gpio_init(void);

void set_led_connect(uint32_t bit);
void set_led_running(uint32_t bit);
uint32_t get_led_connect(void);
uint32_t get_led_running(void);

static inline uint32_t dap_get_time_stamp(void)
{
    return (uint32_t)xTaskGetTickCount();
}
